module Texture {
	export class ResTest extends util.ILoadInterface {
		private _txInfo:egret.TextField;
		private _loadingUI:Component.LoadingUI;

		/**
		 * 构造函数
		 */
		public constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
		}

		/**
		 * 添加到舞台监听器
		 */
		private onAddToStage(event: egret.Event) {			
            //背景
            var bg: egret.Shape = new egret.Shape;
            bg.graphics.beginFill(0x338888, 1.0);
            bg.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
            bg.graphics.endFill();
            this.addChildAt(bg,0);
			//加载进度组件
			this._loadingUI=new Component.LoadingUI(0,10);
			this._loadingUI.x=this.stage.stageWidth/2;
			this._loadingUI.y=this.stage.stageHeight/2;
			this.addChild(this._loadingUI);
			//预加载资源组
			var loadUtil:util.LoadGroupUtil=util.LoadGroupUtil.getInstance(this._loadingUI);
			loadUtil.preLoadGroup(this);
		}

		/**
		 * @override
		 * 资源组完成回调
		 */
		public loadGroupCallback():void{
			/// 提示信息
			this._txInfo = new egret.TextField;
			this._txInfo.size = 28;
			this._txInfo.textAlign = egret.HorizontalAlign.CENTER;
			this._txInfo.textColor = 0x843900;
			this._txInfo.lineSpacing = 6;
			this._txInfo.multiline = true;
            this._txInfo.text = "点击按钮加载资源\n红色按钮同步加载资源\n绿色按钮异步加载资源\n蓝色按钮使用URL加载资源\n黄色按钮使用Loader加载资源";
            this._txInfo.width = this.stage.stageWidth;
			this._txInfo.y = 120;
            this.addChild(this._txInfo);
			//移动加载进度组件
			if(this.contains(this._loadingUI)){
				this.removeChild(this._loadingUI);
			}
			//添加四个按钮
			var btn1:egret.Shape=new egret.Shape;
			btn1.graphics.beginFill(0xff0000);
			btn1.graphics.drawRect(0,0,100,60);
			btn1.graphics.endFill();
			btn1.x=50;
			btn1.y=100;
			this.addChild(btn1);
			var btn2:egret.Shape=new egret.Shape;
			btn2.graphics.beginFill(0x00ff00);
			btn2.graphics.drawRect(0,0,100,60);
			btn2.graphics.endFill();
			btn2.x=50;
			btn2.y=180;
			this.addChild(btn2);
			var btn3:egret.Shape=new egret.Shape;
			btn3.graphics.beginFill(0x0000ff);
			btn3.graphics.drawRect(0,0,100,60);
			btn3.graphics.endFill();
			btn3.x=50;
			btn3.y=260;
			this.addChild(btn3);
			var btn4:egret.Shape=new egret.Shape;
			btn4.graphics.beginFill(0xffff00);
			btn4.graphics.drawRect(0,0,100,60);
			btn4.graphics.endFill();
			btn4.x=50;
			btn4.y=340;
			this.addChild(btn4);
			btn1.touchEnabled=btn2.touchEnabled=btn3.touchEnabled=btn4.touchEnabled=true;
			btn1.name="res";
			btn2.name="asyn";
			btn3.name="url";
			btn4.name="loader";
			btn1.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touchHandler,this);
			btn2.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touchHandler,this);
			btn3.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touchHandler,this);
			btn4.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touchHandler,this);
		}

		/**
		 * 触摸事件
		 */
		private touchHandler(evt:egret.TouchEvent):void{
			var target=evt.target.name;
			egret.log(target);
			switch(target){
				case "res":
					this.loadRes();
					break;
				case "asyn":
					this.loadResAsyn();
					break;
				case "url":
					this.loadResWithURL();
					break;
				case "loader":
					this.loadResWithLoader();
					break;
				default:
					break;
			}
		}

		/**
		 * 同步加载资源
		 */
		private loadRes():void{
			var hero:egret.Bitmap=new egret.Bitmap(RES.getRes("hero_png"));
			this.addChild(hero);
			hero.x=this.stage.stageWidth*Math.random();
			hero.y=this.stage.stageHeight*Math.random();
			hero.anchorOffsetX=hero.width/2;
			hero.anchorOffsetY=hero.height/2;
		}

		/**
		 *异步加载资源
		 */
		private loadResAsyn():void{
			RES.getResAsync("egret_icon",(data:any,key:string)=>{
				var icon:egret.Bitmap=new egret.Bitmap(data);
				egret.log(data,key);
				this.addChild(icon);
				icon.x=200;
			},this);
		}

		/**
		 * 使用URL直接加载资源
		 */
		private loadResWithURL():void{

		}

		/**
		 * 使用loader加载资源
		 */
		private loadResWithLoader():void{

		}

	}
}